// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/metrics/metric.h>
#include <vespa/vespalib/util/jsonstream.h>

namespace metrics {

class JsonWriter : public MetricVisitor, public vespalib::JsonStreamTypes {
    vespalib::JsonStream& _stream;
    enum Flag { NOT_STARTED, SNAPSHOT_STARTED, METRICS_WRITTEN };
    Flag _flag;
    // Not a "true" set, but dimensions are guaranteed to be unique for any
    // given metric set.
    using DimensionSet = Metric::Tags;

    std::vector<DimensionSet> _dimensionStack;
    uint64_t _period;

public:
    JsonWriter(vespalib::JsonStream&);

private:
    bool visitSnapshot(const MetricSnapshot&) override;
    void doneVisitingSnapshot(const MetricSnapshot&) override;
    bool visitMetricSet(const MetricSet&, bool autoGenerated) override;
    void doneVisitingMetricSet(const MetricSet&) override;
    bool visitCountMetric(const AbstractCountMetric&, bool autoGenerated) override;
    bool visitValueMetric(const AbstractValueMetric&, bool autoGenerated) override;
    void doneVisiting() override;

    void checkIfArrayNeedsToBeStarted();
    void writeCommonPrefix(const Metric& m);
    void writeCommonPostfix(const Metric& m);

    void pushLegacyDimensionFromSet(const MetricSet&);
    void pushAllDimensionsFromSet(const MetricSet&);
    void writeDimensions(const DimensionSet&);
    void writeInheritedDimensions();
    void writeMetricSpecificDimensions(const Metric&);
    
    bool isLeafMetric(const Metric& m) const { return !m.isMetricSet(); }
};

}

